package com.ambow.servlet;

import java.io.IOException;

import javax.imageio.spi.RegisterableService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.tomcat.util.descriptor.web.LoginConfig;
import org.hibernate.Session;

import com.ambow.entity.User;
import com.ambow.service.UserService;
import com.ambow.service.impl.UserServiceImpl;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	UserService userService = new UserServiceImpl();
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String status = request.getParameter("status");//判断做什么操作
		if(status.equals("login")) {
			login(request,response);
		}else if(status.equals("register")) {
			register(request,response);
		}else if(status.equals("exit")){
			exit(request,response);
		}else if(status.equals("update")) {
			update(request,response);
		}
	}
	//更新
	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id = Integer.valueOf(request.getParameter("id"));
		String account = request.getParameter("account");
		String name = request.getParameter("name");
		String address = request.getParameter("address");
		String tel = request.getParameter("tel");
		System.out.println(id+account+name+address);
		User user = userService.queryById(User.class, id);
		user.setAccount(account);
		user.setAddress(address);
		user.setName(name);
		user.setTel(tel);
		boolean b = userService.update(user);
		if(b) {
			System.out.println("更新成功");
			user = userService.queryById(User.class, id);
			request.getSession().setAttribute("login", user);
			request.getRequestDispatcher("pages/user.jsp").forward(request, response);
		}

	}

	//查看个人信息
	private void query(HttpServletRequest request, HttpServletResponse response) {
		Integer id = Integer.valueOf(request.getParameter("id"));//被查询用户的id
		User user = userService.queryById(User.class, id);
	}

	//退出
	private void exit(HttpServletRequest request, HttpServletResponse response) {
		  request.getSession().invalidate();
		  try {
			response.sendRedirect("index.jsp");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//注册
	public void register(HttpServletRequest request, HttpServletResponse response) {
		try {

			String account = request.getParameter("account");//账号
			String name = request.getParameter("name");//姓名
			String pwd = request.getParameter("pwd");//密码
			String address = request.getParameter("address");//收获地址
			String tel = request.getParameter("tel");//联系电话
			String degree = "user";
			User user = new User();
			user.setAccount(account);
			user.setAddress(address);
			user.setDegree(degree);
			user.setName(name);
			user.setPwd(pwd);
			user.setTel(tel);
			user.setStatus(1);
			boolean b = userService.add(user);
			if (b) {
				System.out.println("注册成功");
				request.getRequestDispatcher("pages/account.jsp").forward(request, response);
			}else {
				response.sendRedirect("pages/register.jsp");
			}
		} catch (Exception e) {
		}
		
		
	}

	//验证登录
	public void login(HttpServletRequest request, HttpServletResponse response) {
		try {
		String account = request.getParameter("account");//账号
		String pwd = request.getParameter("pwd");// 密码
		String degree = request.getParameter("degree");//用于区分是用户还是管理员
		System.out.println(degree+"jjjjjjjjjjjjj");
		String hql = "from User where account = ? and pwd = ? and degree = ?";
		String[] param = { account, pwd, degree };
		User user = userService.login(hql, param);
		if (user!=null) {
			System.out.println("成功登录"+user.getId()+"999999"+user.getAccount());
			HttpSession session = request.getSession();
			session.setAttribute("login", user);
			//session.setAttribute("degree", degree);
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}else {
			System.out.println("登录失败");
			response.sendRedirect("pages/account.jsp");
		}
		} catch (Exception e) {
		}
	}

}
